package view;

import entity.CourseDO;
import entity.GradeDO;
import handler.UpdateGradeViewHandler;
import service.CourseService;
import service.GradeService;
import service.impl.CourseServiceImpI;
import service.impl.GradeServiceImpI;

import javax.swing.*;
import java.awt.*;

public class UpdateGradeView extends JDialog {

    JPanel jPanel = new JPanel();
    //    JLabel snoLabel = new JLabel("学号");
//    JTextField snoTxt = new JTextField();
    JLabel IDLabel = new JLabel("ID");
    JTextField IDTxt = new JTextField();
    JLabel nameLabel = new JLabel("学号");
    JTextField nameTxt = new JTextField();

    JLabel snoLabel = new JLabel("课程号");
    JTextField snoTxt = new JTextField();

    JLabel sexLabel = new JLabel("分数");
    JTextField sexTxt = new JTextField();

    JLabel ageLabel = new JLabel("等级");
    JTextField ageTxt = new JTextField();


    JLabel deptLabel = new JLabel("学期");
    JTextField deptTxt = new JTextField();

    UpdateGradeViewHandler updateGradeViewHandler;
    //
    JButton updateBtn = new JButton("修改");
//
//    UpdateStudentViewHandler updateStudentViewHandler;

    public UpdateGradeView(GradeView gradeView, int selectCourseId) {
        super(gradeView, "修改成绩信息", true);
        //设置监听
        updateGradeViewHandler = new UpdateGradeViewHandler(this, gradeView);
        //查询对应记录并回显
        GradeService gradeService = new GradeServiceImpI();
        GradeDO selectCourse = gradeService.getById(selectCourseId);

        IDLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(IDLabel);
        IDTxt.setPreferredSize(new Dimension(200, 30));
        IDTxt.setText(selectCourse.getId() + "");
        IDTxt.setEnabled(false);
        jPanel.add(IDTxt);

        snoLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(snoLabel);
        snoTxt.setPreferredSize(new Dimension(200, 30));
        snoTxt.setText(selectCourse.getSno() + "");
        //id设置为不可编辑
        snoTxt.setEnabled(false);
        jPanel.add(snoTxt);

        nameLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(nameLabel);
        nameTxt.setPreferredSize(new Dimension(200, 30));
        nameTxt.setText(String.valueOf(selectCourse.getCno()));
        nameTxt.setEnabled(false);
        //nameTxt.setEnabled(false);
        jPanel.add(nameTxt);

        sexLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(sexLabel);
        sexTxt.setPreferredSize(new Dimension(200, 30));
        sexTxt.setText(selectCourse.getGrade());
        //sexTxt.setEnabled(false);
        jPanel.add(sexTxt);

        ageLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(ageLabel);
        ageTxt.setPreferredSize(new Dimension(200, 30));
        ageTxt.setText(String.valueOf(selectCourse.getLevel()));
        //ageTxt.setEnabled(false);
        jPanel.add(ageTxt);

        deptLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(deptLabel);
        deptTxt.setPreferredSize(new Dimension(200, 30));
        deptTxt.setText(selectCourse.getSemester());
        jPanel.add(deptTxt);


        updateBtn.addActionListener(updateGradeViewHandler);
        jPanel.add(updateBtn);

        Container contenPane = getContentPane();
        contenPane.add(jPanel);

        setSize(600, 400);//大小
        setVisible(true);//可以看见
        setResizable(false);//不可变大小
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//退出自动关闭
        setLocationRelativeTo(null);//居中
    }

    //获取修改后的对象
    public GradeDO buildUpdateGradeDO() {
        GradeDO gradeDO = new GradeDO();
        gradeDO.setSno(Integer.valueOf(snoTxt.getText()));
        gradeDO.setCno(Integer.valueOf(nameTxt.getText()));
        gradeDO.setGrade(sexTxt.getText());
        gradeDO.setLevel(ageTxt.getText());
        gradeDO.setSemester(deptTxt.getText());
        return gradeDO;
    }
}
